/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * $Id: AlarmClient.hpp,v 1.6 2009-05-13 07:18:16 amaula Exp $
 *
 * \file
 * \brief MaCI - Alarm Interface Client header
 * \author Antti Maula <antti.maula@tkk.fi>
 *
 * TODO:
 * - competences = tasklibrary
 * - get tasks = getpool
 * - removetaskfromlibrary function
 * - Add/RemoveTaskToPool/library - functions
 * - Add taskID to GetTasks function
 * - Error message when state is error
 */
#ifndef _MACI_INTERFACE_ALARMCLIENT_HPP_
#define _MACI_INTERFACE_ALARMCLIENT_HPP_

#include "MaCI.hpp"
//#include "thread.hpp"
//#include "sync.hpp"

// These are not necessarily required here, but defined to make the
// interface easier to include.
#include "AlarmData.hpp"

#include <list>

// Forward declare gimi::GIMI
namespace gimi {
  class GIMI;
}

namespace MaCI {
  namespace Alarm {
    
    // Forward declarations
    class CAlarmData;
    class CAlarmClient;

    
    /** Callback interface class for Alarm interface.
     *
     * This interface defines functions which can be used for
     * AlarmClient callbacks. These functions are executed (more
     * detail per function basis) as new data arrives, and directly
     * from the context of the receiving thread. <b>Therefore, it is
     * important that the handling function is as simple and fast as
     * possible, as it will block the execution of the receiving
     * thread while executed.</b>
     */
    class CAlarmClientCallback
    {
    public:

      CAlarmClientCallback() {}
      virtual ~CAlarmClientCallback() {}


      /** Handler function for AlarmData event.
       *
       * This function is called when the AlarmClient receives new
       * data from the server.
       *
       * \note <b>Keep this function simple & fast as it will block the 
       * data receiving thread while executing!</b>
       *
       * @param[in] aData       Constant reference to CAlarmData element 
       *                        containing the received event.
       * @param[in] aDataSequence Sequence number of the received data. 
       *                        This should increment by one on each call.
       * @param[in] aAlarmClient Reference to AlarmClient 
       *                        instance which received this event.
       */
      virtual void OnAlarmDataEvent(const CAlarmData &aData, 
                                    const unsigned int aDataSequence,
                                    CAlarmClient &aAlarmClient) = 0;
    };
    
    /** Alarm event types.
     */
    enum EAlarmEvent {
      KEventAlarmData = 0 ///< AlarmData element received and stored.
    };

    /** MaCI - Alarm Client interface.
     * 
     * This class contains the methods and types for using
     * a MaCI::Alarm service in the GIMnet.
     */
    class CAlarmClient 
      : private gim::CSync,
        private gim::CThread,
        public MaCI::CMaCI
    {
    public:
      /** Constructor.
       * 
       * Constructor of the AlarmClient. This requires;
       * a pointer to Initialized and connected GIMI, minor
       * number of this interface (Used as GIMI minor number)
       *
       * @param[in] aGIMIPtr Pointer to GIMI used for communication
       * @param[in] aInterfaceMinor Interface minor number. Usually
       *            used to indicate the instance of devices of same
       *            type.
       */     
      CAlarmClient(gimi::GIMI *aGIMIPtr, const int aInterfaceMinor = -1);

      
      /** Destructor.
       */
      ~CAlarmClient();

      
      /** Receive AlarmEvent.
       *
       * @param[out] aData     Reference to CAlarmData to copy the data to.
       * @param aSequence      Pointer to sequence number. If this is NULL,
       *                       the most recent position is returned immediately.
       *                       If this is not NULL, the value pointed
       *                       should be set to 0 on the first call to this function.
       *                       After each call, the sequence number of the returned
       *                       data is stored here. If user wants to read all the
       *                       measures available, the value pointed should not be
       *                       modified after the first call.
       * @param[in] aTimeout_ms Timeout value. If the operation is not completed
       *                       before timeout is reached, the operation fails.
       * @return               true - Operation was succesfull, new data is copied
       *                       to provided location.
       *                       false - Operation failed; timeout or generic error
       *                       encountered.
       */
      bool GetAlarmEvent(CAlarmData &aData,
                         const unsigned int aTimeout_ms);


      /** Send Alarm Command.
       *
       */
      bool SendCommand(const CAlarmData &aCmd);
      
      
      /** Assigns the Alarm client callback function. 
       *
       * This sets a function object for calling a position client
       * callback. If this is set, the interface calls the assigned
       * functions immediately when event is available.
       * 
       * \note As the function is run in the context of the receiving
       * thread, user must make sure that the callback function is
       * fast. As if the function executes too long, receiving events
       * is interrupted.
       *
       * @param[in] aAlarmClientCallback Pointer to class implementing the interface
       *                                 CAlarmClientCallback.
       */
      void SetAlarmClientCallback(CAlarmClientCallback *aAlarmClientCallback) {
        iAlarmClientCallback = aAlarmClientCallback;
      }
      
      
    protected:
      /** Open client interface.
       */
      EMaCIError DoOpen(void);
      
      
      /** Close client interface.
       */
      EMaCIError DoClose(void);


    private:
      /// Thread runner function. Is in charge of receiving events.
      int ThreadFunction(const int aThreadNumber);
      int ProcessMessage(const gimi::GIMIMessage &msg);
      
      //data.
      unsigned int iAlarmSequence;
      std::list<CAlarmData> iAlarmDataArray;
      CAlarmClientCallback *iAlarmClientCallback; ///< Pointer to class instance containing callback functions.

    private:
      CAlarmClient(const CAlarmClient &) 
        : CSync(2,1),
          CThread(1),
          CMaCI(NULL, GIMI_PROTOCOL_MACI_ALARM, 0, ""),
          iAlarmSequence(),
          iAlarmDataArray(),
          iAlarmClientCallback() {}
      CAlarmClient &operator=(const CAlarmClient &) { return *this; }
    };


  }
}

#endif //_MACI_INTERFACE_ALARMCLIENT_HPP_
